package ar.com.sebasoft.rembrandt;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import ar.com.sebasoft.rembrandt.util.SystemHelper;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import com.actionbarsherlock.view.MenuItem;

public class GalleryListActivity extends SherlockFragmentActivity implements
		GalleryListFragment.Callbacks {

	private boolean mDosPaneles;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.activity_gallery_list);

		SystemHelper.setCurrentContext(this.getApplicationContext());

		if (this.findViewById(R.id.gallery_detail_container) != null) {
			this.mDosPaneles = true;
			Log.d(SystemHelper.APP_TAG, "Using two panes");
			((GalleryListFragment) this.getSupportFragmentManager()
					.findFragmentById(R.id.gallery_list))
					.setActivateOnItemClick(true);
		} else {
			Log.d(SystemHelper.APP_TAG, "Using one pane");
		}
		SystemHelper.setCurrentContext(this);

	}

	@Override
	public void onItemSelected(String id) {
		if (this.mDosPaneles) {
			Bundle argumentos = new Bundle();
			argumentos.putString(GalleryDetailFragment.ARG_ALBUM_ID, id);
			GalleryDetailFragment fragment = new GalleryDetailFragment();
			fragment.setArguments(argumentos);
			this.getSupportFragmentManager().beginTransaction()
					.replace(R.id.gallery_detail_container, fragment).commit();
		} else {
			Intent intentoDetalle = new Intent(this,
					GalleryDetailActivity.class);
			intentoDetalle.putExtra(GalleryDetailFragment.ARG_ALBUM_ID, id);
			this.startActivity(intentoDetalle);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater infla = getSupportMenuInflater();
		infla.inflate(R.menu.menu_prefs, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Intent intent = null;
		switch (item.getItemId()) {
		case R.id.mnu_itm_prefs:
			intent = new Intent(this, SettingsActivity.class);
			startActivity(intent);
			return true;
		case R.id.mnu_itm_about:
			intent = new Intent(this, AboutMeActivity.class);
			startActivity(intent);
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}
